package com.studentsystem.service.impl;

import com.studentsystem.dao.UserDao;
import com.studentsystem.dataobject.UserDO;
import com.studentsystem.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (User)表服务实现类
 */
@Service("userService")
public class UserServiceImpl implements UserService {
    @Resource
    private UserDao userDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 用户名
     * @return 实例对象
     */
    @Override
    public UserDO queryById(String id) {
        return this.userDao.queryById(id);
    }

    @Override
    public UserDO queryByUsername(String username) {
        return this.userDao.queryByUsername(username);
    }

    /**
     * 通过ID和PWD查询单条数据
     *
     * @param username 用户名
     * @param pwd 密码
     * @return 实例对象
     */
    @Override
    public UserDO queryByUsernameAndPwd(String username, String pwd) {
        return this.userDao.queryByUsernameAndPwd(username, pwd);
    }

    @Override
    public List<UserDO> getUsersByRole(String role) {
        UserDO userDO = new UserDO();
        userDO.setRole(role);
        return this.userDao.queryAll(userDO);
    }

    @Override
    public List<UserDO> getUsersByParamAndRole(String param, String role) {
        return this.userDao.queryByParamAndRole(param, role);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<UserDO> queryAllByLimit(int offset, int limit) {
        return this.userDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param userDO 实例对象
     * @return 实例对象
     */
    @Override
    public UserDO insert(UserDO userDO) {
        this.userDao.insert(userDO);
        return userDO;
    }

    /**
     * 修改数据
     *
     * @param userDO 实例对象
     * @return 实例对象
     */
    @Override
    public Boolean update(UserDO userDO) {
        return this.userDao.update(userDO) > 0;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.userDao.deleteById(id) > 0;
    }
}